package com.zh.test07IO;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException {


        while (true){
        System.out.println("-------hello--------");
        System.out.println("1.展示书籍");
        System.out.println("2.增加书籍");
        System.out.println("3.删除书籍");
        System.out.println("4.退出应用");


        //借助 Scanner类
        Scanner sc = new Scanner(System.in);
        //利用键盘录入序号  需要提示
        System.out.println("请输入想要执行功能的序号：");
        int choice = sc.nextInt();
        if (choice == 1){
            System.out.println("---------1.展示书籍");
            //流
            File f =new File("F:\\W\\demo.txt");

            if(f.exists() == true){//文件存在
                FileInputStream fis = new FileInputStream(f);
                ObjectInputStream ois = new ObjectInputStream(fis);
                //读取
                ArrayList list = (ArrayList)(ois.readObject());

                for (int i=0; i <= list.size() - 1; i++){
                    Book b = (Book) (list.get(i));
                    System.out.println(b.getbNo() + "----" + b.getbName() + "----" + b.getbAuthor());
                }

            }else {
                System.out.println("当前没有书籍");
            }





        }
            if (choice == 2){
                System.out.println("---------2.增加书籍");
                System.out.println("请输入编号，书名，作者");
               int bNo = sc.nextInt();
               String bName = sc.next();
               String bAuthor = sc.next();
                Book b = new Book();
                b.setbNo(bNo);
                b.setbName(bName);
                b.setbAuthor(bAuthor);

                File f = new File("F:\\W\\demo.txt");
                if(f.exists() == true){//文件存在
                    //流
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    //读取
                    ArrayList list = (ArrayList)(ois.readObject());
                    list.add(b);

                    //需要流
                    FileOutputStream fos = new FileOutputStream(f);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);

                    //将list写出去
                    oos.writeObject(list);

                    //关闭流
                    oos.close();


//                    for (int i=0; i <= list.size() - 1; i++){
//                        Book b = (Book) (list.get(i));
//                        System.out.println(b.getbNo() + "----" + b.getbName() + "----" + b.getbAuthor());
//                    }

                }else {
                    //创建一个空集合
                ArrayList list = new ArrayList();
                list.add(b);
                    //需要流
                    FileOutputStream fos = new FileOutputStream(f);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);

                    //将list写出去
                    oos.writeObject(list);

                    //关闭流
                    oos.close();

                }


//                //创建一个空集合
//                ArrayList list = new ArrayList();
//                list.add(b);
//                //需要流
//                FileOutputStream fos = new FileOutputStream(f);
//                ObjectOutputStream oos = new ObjectOutputStream(fos);
//
//                //将list写出去
//                oos.writeObject(list);
//
//                //关闭流
//                oos.close();




            }
            if (choice == 3){
                System.out.println("---------3.删除书籍");
                //录入想要删除书籍编号
                System.out.println("请输入想要删除的编号，如果想取消删除请按010");
              int delNo = sc.nextInt();
              if (delNo == 010){
                  break;
              }

                //流
                File f =new File("F:\\W\\demo.txt");
                if (f.exists()) { // 文件存在
                    try (FileInputStream fis = new FileInputStream(f);
                         ObjectInputStream ois = new ObjectInputStream(fis)) {
                        ArrayList<?> list = (ArrayList<?>) ois.readObject();

                        // 下架对应书籍
                        for (int i = 0; i < list.size(); i++) {
                            Book b = (Book) list.get(i);
                            if (b.getbNo() == delNo) { // 确保getbNo()返回的类型与delNo匹配
                                list.remove(i);
                                System.out.println("删除成功");
                                break;
                            }
                        }

                        // 重新将修改后的列表写回到文件中
                        try (FileOutputStream fos = new FileOutputStream(f);
                             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                            oos.writeObject(list);
                        }

                    } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                } else {
                    System.out.println("文件不存在或已经没有数据删了");
                }


                //下架对应书籍
//                for (int i=0; i <= list.size() - 1; i++){
//                    Book b = (Book) (list.get(i));
//                    if (b.getbNo() == delNo){
//                        list.remove(b);
//                        System.out.println("删除成功");
//                        break;  //
//                    }
//                }

            }
            if (choice == 4){
                System.out.println("---------4.退出应用");
                break;
            }
        }




        }



    }


